package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.base.SmartBeep;
import com.moleq.device.DeviceManager;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.enums.ItemType;
import com.moleq.logic.MoleQConstants;
import com.moleq.logic.TransactionManager;
import com.moleq.models.CouponSetting;
import com.moleq.models.Departmant;
import com.moleq.models.FreeFuncRpt;
import com.moleq.models.GlobalInfo;
import com.moleq.mgpos.activities.R;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class GPCTypeSelectionDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private Button mButtonBack,mButtonOk;
	private TransactionManager mPaymentMgr;
	private Context mContext;
	private ListView mListView;
	private List<String> mDataSource = new  ArrayList<String>();
	private SelectionAdapter mAdapter;
	private int selectedRow = 0;
//	private static final String INCREASE_PRICE_BY_PER = "Price + by %";
//	private static final String DECREASE_PRICE_BY_PER = "Price - by %";
//	private static final String INCREASE_AMOUNT = "Price + by Amount";
//	private static final String DECREASE_AMOUNT = "Price - by Amount";
//	private static final String REPLACE_THE_PRICE = "Replace Price";
//	private static final String DISCOUNT = "Discount";
//	private static final int DIALOG_PRICE_TYPE_INFO = 2;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {	
			switch(v.getId()){
			case R.id.btn_dialog_cancel:
				dismiss();
				break;
			case R.id.btn_dialog_ok:	
				if(mDataSource.size() <= 0){
					return;	
				}
				onCloseWithReturn(mDataSource.get(selectedRow));
				//mDeviceManager.enableAllScanner();
				dismiss();
				break;
			}
		}
	};
	
	public void onCloseWithReturn(Object obj){
		mOnResultListener.onReturnValue(MoleQConstants.DIALOG_PRICE_TYPE_INFO,obj);
		dismiss();
	}
	
	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {


		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_dept_list:
//				ScrollToRow(position);
				selectedRow = position;
				mAdapter.ScrollToRow(position);
				break;
			}
		}
	};
	
	
	public GPCTypeSelectionDialog(Context context) {
		super(context,context);
		constructor(context);
		
	}

	public GPCTypeSelectionDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	public GPCTypeSelectionDialog(Context context, Object base) {
		super(context,base);
		constructor(context,base);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.price_dept_dialog_perview_models);
		this.setCancelable(false);
	
		mButtonBack = (Button) findViewById(R.id.btn_dialog_cancel);
		mButtonBack.setOnClickListener(mOnClickListener);
		
		mButtonOk = (Button) findViewById(R.id.btn_dialog_ok);
		mButtonOk.setOnClickListener(mOnClickListener);

		mListView = (ListView) findViewById(R.id.lv_dialog_dept_list);
		mListView.setOnItemClickListener(onItemClickListener);
		
		
//		mListView.setDivider(null);
		
		mDataSource.add(MoleQConstants.GPC_INCREASE_PRICE_BY_PER);
		mDataSource.add(MoleQConstants.GPC_DECREASE_PRICE_BY_PER);
		mDataSource.add(MoleQConstants.GPC_INCREASE_AMOUNT);
		mDataSource.add(MoleQConstants.GPC_DECREASE_AMOUNT);
		mDataSource.add(MoleQConstants.GPC_REPLACE_THE_PRICE);
		mDataSource.add(MoleQConstants.GPC_DISCOUNT);
	}
	
	public void show(){
		super.show();		
		
		mPaymentMgr =  TransactionManager.getInstance();
		
		mAdapter = new SelectionAdapter(mContext,mDataSource);
		mListView.setAdapter(mAdapter);
		//mAdapter.notifyDataSetChanged();
	}
	
	/*
	class OpenCouponAdapter extends BaseAdapter{
		private Context maContext;
		public final class ViewHolder{
			public TextView tv_itemsname;
//			public TextView tv_coupon_value;
//			public TextView tv_coupon_min_purchase;
		}
		
		public OpenCouponAdapter(Context context){
			maContext = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataSource.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataSource.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if(convertView == null){
				holder = new ViewHolder();
				
				convertView = LayoutInflater.from(maContext).inflate(R.layout.dept_chooseitem, null);
				holder.tv_itemsname = (TextView) convertView.findViewById(R.id.tv_itemsname);

//				convertView.setOnTouchListener(onItemTouchListener);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.tv_itemsname.setText(mDataSource.get(position));
			
			if(position == selectedRow){
				holder.tv_itemsname.setBackgroundColor(Color.rgb(255,219,16));
			}else{
				holder.tv_itemsname.setBackgroundColor(Color.rgb(204, 204, 204));
			}
//			
			return convertView;
		}
		
	}
	*/
	
//	private OnTouchListener onItemTouchListener = new OnTouchListener(){
//
//		@Override
//		public boolean onTouch(View v, MotionEvent event) {
//			if(event.getAction() == MotionEvent.ACTION_DOWN){
//				SmartBeep.getInstance().play(SmartBeep.SOUND_CLICK);
//			}
//			return false;
//		}
//		
//	};
	
//	public void ScrollToRow(int row){
//		selectedRow = row;
//		mAdapter.notifyDataSetChanged();
//	}


}
